//
//  RouDate.swift
//  ShangYouNews
//
//  Created by wangkun on 2017/5/21.
//  Copyright © 2017年 baymax. All rights reserved.
//

import UIKit

public enum DateType {
    case year
    case month
    case day
    case hour
    case minute
    case second
}

extension Date {
    
    /// Date转字符串
    ///
    /// - Parameter format: 格式
    /// - Returns: 转化后的时间字符串
    public func toString(format: String) -> String {
        let dateFormate: DateFormatter = DateFormatter()
        dateFormate.dateFormat = format
        return dateFormate.string(from: self)
    }
    
    /// 时间偏移
    ///
    /// - Parameters:
    ///   - type:   偏移对象(年、月、日...)
    ///   - amount: 偏移量
    ///   - format: 时间格式化字符串
    /// - Returns: Date或者格式化之后的时间
    public func deviation<T>(type: DateType, amount: Int, format: String = "yyyy-MM-dd",  _ completeBlock: (_ date: T)->Void) {
        var comps = DateComponents()
        switch type {
        case .year:
            comps.year = amount
            break
        case .month:
            comps.month = amount
            break
        case .day:
            comps.day = amount
            break
        case .hour:
            comps.hour = amount
            break
        case .minute:
            comps.minute = amount
            break
        case .second:
            comps.second = amount
            break
        }
        
        let calender = NSCalendar(calendarIdentifier: .gregorian)
        let date = calender?.date(byAdding: comps, to: self, options: NSCalendar.Options(rawValue: 0))
        
        if T.self == Date.self {
            completeBlock(date as! T)
        } else {
            completeBlock(date?.toString(format: format) as! T)
        }
        
    }
    
    /// 时间转成文字
    /// - Parameter format: 格式
    /// - Returns: 转换后的字符串
    public func timeConversion(format: String = "MM-dd HH:mm") -> String {
        
        let currentDate: Date = Date.init(timeIntervalSinceNow: 0)
        let calendar: NSCalendar = NSCalendar.current as NSCalendar
        let dateComponents: DateComponents = calendar.components([.day,.hour,.minute,.minute], from: self, to:currentDate , options: .init(rawValue: 0))
        if dateComponents.day ?? 0 > 0{
            return self.toString(format: format)
        } else if dateComponents.hour ?? 0 > 0 {
            return "\(dateComponents.hour ?? 0)小时以前"
        } else if dateComponents.minute ?? 0 > 30 {
            return "\(dateComponents.minute ?? 0)分钟前"
        } else {
            return "刚刚"
        }
        
    }
    
    /**
     *  var date = Date()  2012 xx xx
     *  date.adding(.year,value:10) 2022 xx xx xx
     *
     */
    public func adding(_ component : Calendar.Component ,value : Int) ->Date {
        return Calendar.current.date(byAdding: component, value: value, to: self)!
    }
    
    
    /** 日历*/
    public var calendar : Calendar{
        return Calendar.current
    }
    
    /** Date().era -> 1*/
    public var era : Int{
        return Calendar.current.component(.era, from: self)
    }
    
    /** 季度*/
    public var quarter : Int{
        return Calendar.current.component(.quarter, from: self)
    }
    
    /** week of year*/
    public var weekOfYear : Int{
        return Calendar.current.component(.weekOfYear, from: self)
    }
    
    /** 年份*/
    public var year: Int {
        get {
            return Calendar.current.component(.year, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .year, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** 月份*/
    public var month: Int {
        get {
            return Calendar.current.component(.month, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .month, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** 天*/
    public var day: Int {
        get {
            return Calendar.current.component(.day, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .day, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** 星期几*/
    public var weekday: Int {
        get {
            return Calendar.current.component(.weekday, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .weekday, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** 小时*/
    public var hour: Int {
        get {
            return Calendar.current.component(.hour, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .hour, value: newValue, of: self) {
                self = date
            }
        }
    }
    
   /** 分钟*/
    public var minute: Int {
        get {
            return Calendar.current.component(.minute, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .minute, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** 秒*/
    public var second: Int {
        get {
            return Calendar.current.component(.second, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .second, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** nanoSecond*/
    public var nanosecond: Int {
        get {
            return Calendar.current.component(.nanosecond, from: self)
        }
        set {
            if let date = Calendar.current.date(bySetting: .nanosecond, value: newValue, of: self) {
                self = date
            }
        }
    }
    
    /** Milliseconds*/
    public var millisecond: Int {
        get {
            return Calendar.current.component(.nanosecond, from: self) / 1000000
        }
        set {
            let ns = newValue * 1000000
            if let date = Calendar.current.date(bySetting: .nanosecond, value: ns, of: self) {
                self = date
            }
        }
    }
    
    
    
    
    
    
    
    
}

